﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Valor.DBUtility.MSSQLHelper;
using xTeam.HengLe.Entity;
using System.Data.SqlClient;

namespace xTeam.HengLe.DataAccess
{
    public class AdminDataAccess
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private static string _con;
        public static string Con
        {
            get { return _con; }
            set { _con = value; }
        }

        public AdminDataAccess() { }

        /// <summary>
        /// 管理员的添加修改
        /// </summary>
        /// <param name="news">管理员实体</param>
        /// <returns>是否成功</returns>
        public static int Edit(EAdmin admin)
        {
            object[] paramvalues = new object[] { 
                admin.AdminId,admin.AdminName,admin.Password,admin.Isvalid };
            StoreProcedure sp = new StoreProcedure(_con, "admin_update");
            int ret = sp.ExecuteNonQuery(paramvalues);
            return ret;
        }
        /// <summary>
        /// 删除管理员
        /// </summary>
        /// <param name="adminid">管理员编号</param>
        /// <returns>是否成功</returns>
        public static int Delete(int adminid)
        {
            object[] paramvalues = new object[] { adminid };
            StoreProcedure sp = new StoreProcedure(_con, "admin_delete");
            int ret = sp.ExecuteNonQuery(paramvalues);
            return ret;
        }
        /// <summary>
        /// 管理员查询
        /// </summary>
        /// <param name="adminname">登录名(为空查询所有，否则根据登录名查询)</param>
        /// <returns>管理员列表</returns>
        public static List<EAdmin> GetByAdminName(string adminname)
        {
            List<EAdmin> newsList = new List<EAdmin>();
            object[] paramvalues = new object[] { adminname };
            StoreProcedure sp = new StoreProcedure(_con, "admin_select");
            SqlDataReader reader = sp.ExecuteDataReader(paramvalues);

            while (reader.Read())
            {
                newsList.Add(new EAdmin()
                {
                    AdminId = Convert.ToInt32(reader["AdminId"]),
                    AdminName = Convert.ToString(reader["AdminName"]),
                    Password = Convert.ToString(reader["Password"]),
                    WriteTime = Convert.ToDateTime(reader["WriteTime"]),
                    Isvalid = Convert.ToBoolean(reader["Isvalid"])
                });
            }

            reader.Close();
            return newsList;
        }
    }
}
